// ImageProcDummy.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"

#include "ImageExptLibHeaders.h"
#include "ImageProcDummy.h"
#include "DummyOne.h"
#include "ProcParamsInputs.h"
#include "ProcParamsOutputs.h"
#include "IImageProcStep.h"


// This is the constructor of a class that has been exported.
// see ImageProcDummy.h for the class definition
CImageProcDummy::CImageProcDummy() : _DoneCnt(0), _ImageFn('\0')
{
	AddImageFunction (new DummyOne() );

	_Proceed.RedLight();
	_Done.RedLight();
	_thread.Resume ();
	return;
}

CImageProcDummy::~CImageProcDummy()
{
	_ipParams = '\0';
	if (_ImageFn != '\0')
		delete _ImageFn;
	Kill ();
}

bool  CImageProcDummy::AddImageFunction (IImageProcFn *ImageFn )
{
	bool rc = false;

	if ( (ImageFn != '\0') && (ImageFn->IsApiValid()))
	{
		_ImageFn = ImageFn;
		rc = true;
	}

	return rc;
}

void	CImageProcDummy::Setup(const char *FunctionName, ProcParamsOutputs *output, ProcParamsInputs *input) //this means ImageProcDummy must reference ImageExptLib
{
	_ImageFn = '\0';

	if ((_ipParams = input) != '\0')
	{
		if (_ImageFn == '\0')
		{

		}
	}

	if ((_opParams = output) != '\0')
	{
		if (_ImageFn == '\0')
		{
			//set returncode
		}
	}

	//setup variables from input. param 1 = function name	

	//setup mutex - stop, done
	_Proceed.GreenLight();
}

void	CImageProcDummy::Loop()
{
	int fnReturnCode = 0;	//0 == ok

	if (_ImageFn == '\0')
		fnReturnCode = 1;
	else
	{
		try
		{
			_Proceed.Wait();		//wait for thread creater to call Setup and for Setup call _Proceed.GreenLight()

			for ( ; ; )
			{
				//start looptime
				if (_isDying) 		//OnCancel() -> thread being deleted
				{
					fnReturnCode = 2;	//user cancel
					break;
				}
				else
				{
					if (_DoneCnt > 10)
						break;			//image processed		
					else
					{

						_ImageFn->Process('\0', true);		//TODO: implement as a fn pointer according to input param FunctionName - do some useful work like processing a part of the image
						_DoneCnt++;		
					}

				}
			}
		}
		catch (...)
		{
			fnReturnCode = 3; // DummyFnexception
		}
	}
	SetupOutputParams(fnReturnCode);		//abend if caller presses cancel or thread times out

}

void	CImageProcDummy::SetupOutputParams(int returncode)
{
//	Lock lock();

		//set resultCode = fnreturnCode
		//set function specific results
		//	foreach(val in fnresultset)
		//		name.Format("Prefix-%c-%d", val->GetAxis(), val->GetIndex()");	//Prefix-X-1
		//		output->AddParam(new ProcParamString(name, val->getString()));
	_Done.GreenLight();

	return;
}

